//
//  RBSecondKillVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/8/12.
//

#import "RBSecondKillVC.h"
#import "RBSecondKillResponse.h" //model
#import "RBSecondKillListCell.h" //cell
#import "RBSecondKillDetailVC.h" //详情
#import "BaseCollectionView.h"
#import "RBSecondKillTimeCell.h" //cell
#import "RBGoodsDetailVC.h"

@interface RBSecondKillVC ()
<UICollectionViewDataSource, UICollectionViewDelegate,
UICollectionViewDelegateFlowLayout>
{
    BOOL _isFirst;
}
//时间
@property (nonatomic, strong) BaseCollectionView *timeCollectionView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray *dataSource;
@property (nonatomic, retain) NSMutableArray *timesArray;
//请求
@property (nonatomic, strong) RBSecondKillResponse *response;
//时间ID
@property (nonatomic, copy) NSString *interval_id;
//当前秒杀时间段
@property (nonatomic, assign) NSInteger currentIndex;
//
@property (nonatomic, assign) BOOL isCurrent;

@end

@implementation RBSecondKillVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.currentIndex = -1;
    self.isCurrent = YES;
    [self setup];
    //
    LCWeakSelf;
    self.timeCollectionView.CreateCell = ^UICollectionViewCell * _Nonnull(NSIndexPath * _Nonnull indexPath) {
        RBSecondKillTimeCell *cell = [RBSecondKillTimeCell baseCellWith:weakSelf.timeCollectionView indexPath:indexPath];
        //model
        RBSecondKillTimesModel *model = weakSelf.timesArray[indexPath.row];
        if (model) {
            cell.model = model;
        }
        return cell;
    };
    self.timeCollectionView.SelectedCell = ^(NSIndexPath * _Nonnull indexPath) {
        //model
        RBSecondKillTimesModel *model = weakSelf.timesArray[indexPath.row];
        weakSelf.interval_id = model.interval_id;
        weakSelf.isCurrent = indexPath.row == 0;
        [weakSelf getData];
    };
    
    [self getData];
}

#pragma mark - Private

- (void)setup {
    self.title = @"限时秒杀";
    self.view.backgroundColor = [RBThemeConfig bgGrayColor];
    _isFirst = YES;
    self.interval_id = @"";
}

- (void)fillData {
    RBSecondKillModel *allModel = self.response.data;
    [self.timesArray removeAllObjects];
    
    if (_isFirst == YES) {
        _isFirst = NO;
        for (RBSecondKillTimesModel *timeModel in allModel.times) {
            if ([timeModel.is_kill isEqualToString:@"1"]) {
                self.currentIndex = [allModel.times indexOfObject:timeModel];
            }
        }
    }
    for (RBSecondKillTimesModel *timeModel in allModel.times) {
        NSInteger idx = [allModel.times indexOfObject:timeModel];
        if (idx == self.currentIndex) {
            timeModel.isCurrent = YES;
        } else {
            timeModel.isCurrent = NO;
        }
        if (idx >= self.currentIndex) {
            [self.timesArray addObject:timeModel];
        }
    }
    self.timeCollectionView.rows = @[@(self.timesArray.count)];
    [self.timeCollectionView reloadData];
    //
    [self.dataSource removeAllObjects];
    //列表
    [self.dataSource addObject:({
        RBGroupModel *group = RBGroupModel.new;
        group.sectionInset = UIEdgeInsetsMake(Zoom(10), 0, Zoom(10), 0);
        group.minimumLineSpacing = Zoom(10);
        group.cellModels = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            LCWeakSelf;
            for (RBSecondKillListModel *model in allModel.list) {
                model.isCurrent = self.isCurrent;
                [arrayM addObject:({
                    RBCellModel *cellModel = RBCellModel.new;
                    cellModel.cellData = model;
                    cellModel.reuseCellIdentifier = NSStringFromClass(RBSecondKillListCell.class);
                    cellModel.itemSize = CGSizeMake(Zoom(350), Zoom(135));
                    cellModel.cellSelectBlock = ^(NSIndexPath *indexPath) {
                        if (model.isCurrent == YES) {
                            RBSecondKillDetailVC *vc = RBSecondKillDetailVC.new;
                            vc.goods_id = model.id;
                            [weakSelf.navigationController pushViewController:vc animated:YES];
                        } else {
                            RBGoodsDetailVC *vc = RBGoodsDetailVC.new;
                            vc.id = model.goods_id;
                            [weakSelf.navigationController pushViewController:vc animated:YES];
                        }
                    };
                    cellModel;
                })];
            }
            arrayM;
        });
        group;
    })];
    [self.collectionView reloadData];
}

#pragma mark - Network
//网络请求
- (void)getData {
    LCWeakSelf;
    [self viewLoadDataWithParam:^(NSMutableDictionary *dicM) {
        dicM[@"interval_id"] = self.interval_id;
    } path:_Seckill_list post:YES log:YES responseClass:RBSecondKillResponse.class desView:self.view showHud:!self.response showPlaceHolder:NO sucBlock:^(id data) {
        weakSelf.response = data;
        [weakSelf fillData];
    } failBlock:^(RBSecondKillResponse *data) {
        [weakSelf.view showTip:data.message completion:nil];
    } reloadBlock:nil];
}

#pragma mark - Event
//切换时间
- (void)changeTimeData:(UITapGestureRecognizer *)tap {
    RBSecondKillTimesModel *model = tap.view.rbExData;
    self.interval_id = model.interval_id;
    [self getData];
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

kRBCollectionViewDataSourceAndDelegate(self.dataSource);

#pragma mark - Setter

#pragma mark - Getter
//
- (BaseCollectionView *)timeCollectionView {
    if (_timeCollectionView == nil) {
        _timeCollectionView = [[BaseCollectionView alloc] initWithFrame:CGRectZero direction:UICollectionViewScrollDirectionHorizontal itemSize:CGSizeMake(Zoom(65), Zoom(65)) rowMargin:Zoom(10) columnMargin:0 sectionInset:UIEdgeInsetsMake(0, Zoom(13), 0, Zoom(13))];
        _timeCollectionView.backgroundColor = [RBThemeConfig bgGrayColor];
        _timeCollectionView.bounces = NO;
        _timeCollectionView.sections = 1;
        [self.view addSubview:_timeCollectionView];
        [_timeCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.mas_equalTo(0);
            make.width.mas_equalTo(SCREEN_WIDTH);
            make.height.mas_equalTo(Zoom(65));
        }];
    }
    return _timeCollectionView;
}

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        self.collectionView = ({
            UICollectionViewFlowLayout *layout = UICollectionViewFlowLayout.new;
            UICollectionView *view = [UICollectionView.alloc initWithFrame:CGRectZero collectionViewLayout:layout];
            view.showsVerticalScrollIndicator = NO;
            view.showsHorizontalScrollIndicator = NO;
            view.dataSource = self;
            view.delegate = self;
            view.backgroundColor = UIColor.clearColor;
            kRBIgnoreScrollViewInsetsAdjust(view);
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(0);
                make.right.mas_equalTo(0);
                make.bottom.mas_equalTo(0);
                make.top.mas_equalTo(self.timeCollectionView.mas_bottom);
            }];
            view;
        });
    }
    return _collectionView;
}

- (NSMutableArray *)dataSource
{
    if (!_dataSource)
    {
        self.dataSource = ({
            NSMutableArray *arrayM = NSMutableArray.array;
            arrayM;
        });
    }
    return _dataSource;
}

#pragma mark 懒加载
- (NSMutableArray *)timesArray {
    if (_timesArray == nil) {
        _timesArray = [NSMutableArray array];
    }
    return _timesArray;
}

@end
